package per.jdz.demo.service.impl;

import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.row.Db;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;
import per.jdz.demo.dao.HouseDataMapper;
import per.jdz.demo.domain.dto.CommonDTO;
import per.jdz.demo.domain.dto.GeoDTO;
import per.jdz.demo.domain.entity.HouseData;
import per.jdz.demo.service.HouseDataService;
import per.jdz.demo.utils.StrUtil;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CompletableFuture;

import static per.jdz.demo.domain.entity.table.HouseDataTableDef.HOUSE_DATA;

/**
 * @author Jdz
 * @date 2025/1/6
 */
@Service
public class HouseDataServiceImpl extends ServiceImpl<HouseDataMapper, HouseData> implements HouseDataService {

    @Resource
    private HouseDataMapper houseDataMapper;

    @Override
    public List<CommonDTO> selectCommonName(Long countyId) {
        return houseDataMapper.selectCommonName(countyId);
    }

    @Override
    public Long selectCountyId(HouseData houseData) {
        return houseDataMapper.selectCountyId(houseData);
    }

    @Override
    public GeoDTO selectDistance(Long id, BigDecimal lat, BigDecimal lon) {
        return houseDataMapper.selectDistance(id, lat, lon);
    }

    @Override
    public List<Long> selectIdListByGeo(HouseData houseData) {
        return houseDataMapper.selectIdListByGeo(houseData);
    }

    @Override
    public void formatRoomNumber() {
        QueryWrapper query = QueryWrapper.create()
                .select(HOUSE_DATA.CITY, HOUSE_DATA.PROJECT_NAME)
                .groupBy(HOUSE_DATA.CITY, HOUSE_DATA.PROJECT_NAME);
        List<HouseData> list = houseDataMapper.selectListByQuery(query);

        CompletableFuture.allOf(list.stream().map(houseData -> CompletableFuture.runAsync(() -> {
            QueryWrapper q = QueryWrapper.create()
                    .select(HOUSE_DATA.ID, HOUSE_DATA.ROOM_NUMBER)
                    .where(HOUSE_DATA.PROJECT_NAME.eq(houseData.getProjectName()).and(HOUSE_DATA.ROOM.isNull()));
            List<HouseData> houseDataList = houseDataMapper.selectListByQuery(q);
            for (HouseData hd : houseDataList) {
                String roomNumber = hd.getRoomNumber();
                int idx1 = roomNumber.indexOf("(");
                if (idx1 != -1) {
                    roomNumber = roomNumber.substring(0, idx1);
                }
                int idx2 = roomNumber.indexOf("（");
                if (idx2 != -1) {
                    roomNumber = roomNumber.substring(0, idx2);
                }
                roomNumber = roomNumber.replace("--", "-");
                String res = StrUtil.extractNumber(roomNumber);
                if (res == null) continue;
                String[] split = res.split("-");
                hd.setFormatRoomNumber(res);
                hd.setUnitNumber(split[0]);
                hd.setRoom(split[1]);
            }
            Db.updateEntitiesBatch(houseDataList);
        })).toArray(CompletableFuture[]::new)).join();
    }
}
